package edu.cmu.ibill.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.mybeans.dao.DAOException;
import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;


import edu.cmu.ibill.databeans.PersonalBill;
import edu.cmu.ibill.databeans.User;
import edu.cmu.ibill.formbeans.EditPersonalBillForm;
import edu.cmu.ibill.model.CategoryDictDAO;
import edu.cmu.ibill.model.Model;
import edu.cmu.ibill.model.PersonalBillDAO;

public class UpdateBillAction extends Action {
	
	private FormBeanFactory<EditPersonalBillForm> formBeanFactory = FormBeanFactory.getInstance(EditPersonalBillForm.class);
	private PersonalBillDAO personalbillDAO;
	private CategoryDictDAO catDAO;
	
	private SimpleDateFormat format = new SimpleDateFormat( "MM/dd/yyyy" );
	
	public UpdateBillAction(Model model){
		this.personalbillDAO = model.getPersonalBillDAO();
		this.catDAO = model.getCategoryDictDAO();
	}

	@Override
	public String getName() {
		return "updatebill.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		// TODO Auto-generated method stub
		List<String> errors = new ArrayList<String>();
		request.setAttribute("editpersonalbillerrors", errors);
		request.setAttribute("categorydict", catDAO.getAllCategoryNames());
		User user = (User) request.getSession(false).getAttribute("user");	
		try {					
			EditPersonalBillForm form = formBeanFactory.create(request);
			
			request.setAttribute("editid", form.getId());
			request.setAttribute("editdate", form.getDate());
			request.setAttribute("editcategory", form.getCategory());
			request.setAttribute("editamount", form.getAmount());
			request.setAttribute("editcomment", form.getComment());
			
			errors.addAll(form.getValidationErrors());
			if(errors.size()!=0){
				PersonalBill[] pBills = personalbillDAO.LookupByUserId(user.getId());
				request.setAttribute("personalbills", pBills);
				request.setAttribute("editpersonalbillinprocess", "");
				return "manage.jsp";
			}
	        
			PersonalBill personalbill = new PersonalBill();
			
			personalbill.setId(Integer.parseInt(form.getId()));
			personalbill.setAmount(Double.parseDouble(form.getAmount()));
			personalbill.setCategoryId(Integer.parseInt(form.getCategory()));
			personalbill.setDate(new java.sql.Date(format.parse(form.getDate()).getTime()));
			personalbill.setDescription(form.getComment());
			personalbill.setIsShared(false);
			personalbill.setUserId(user.getId());
	        
			personalbillDAO.update(personalbill);
			
			PersonalBill[] pBills = personalbillDAO.LookupByUserId(user.getId());
			request.setAttribute("personalbills", pBills);
			
	        return "manage.jsp";
	        
		} catch (DAOException e) {
			errors.add(e.getMessage());
			PersonalBill[] pBills = personalbillDAO.LookupByUserId(user.getId());
			request.setAttribute("personalbills", pBills);
			request.setAttribute("editpersonalbillinprocess", "");
			return "manage.jsp";
		} catch (FormBeanException e) {
			errors.add(e.getMessage());
			PersonalBill[] pBills = personalbillDAO.LookupByUserId(user.getId());
			request.setAttribute("personalbills", pBills);
			request.setAttribute("editpersonalbillinprocess", "");
			return "manage.jsp";
		} catch (ParseException e) {
			errors.add(e.getMessage());
			PersonalBill[] pBills = personalbillDAO.LookupByUserId(user.getId());
			request.setAttribute("personalbills", pBills);
			request.setAttribute("editpersonalbillinprocess", "");
			return "manage.jsp";
		}
	}

}
